package com.innovation.chengzhangriji.ui;

import android.graphics.Bitmap;
import android.text.TextUtils;

import com.google.gson.JsonElement;
import com.google.zxing.Result;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.preferences.LoginInfo;

public class ConnectDeviceActivity extends com.google.zxing.client.android.CaptureActivity {

    @Override
    public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
        LoginInfo loginInfo = Config.getLoginInfo(this);
        if (loginInfo == null) {
            return;
        }
        String phone = loginInfo.getPhone();
        String token = loginInfo.getToken().getAccessToken();
        String deviceId = rawResult.getText();
        if (TextUtils.isEmpty(deviceId)) {
            return;
        }
        Client.requestConnectDevice(this, phone, token, deviceId, new ResponseHandler(){

            @Override
            public void onInnovationSuccess(JsonElement value) {
                finish();
            }
        }, null);
    }

}
